//
//  HAYJVC.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAYJVC.h"
#import "HAYJMo.h"
#import "HAWebVC.h"
#import "HAHoemAgentVC.h"
#import "HAMyMerchantManageVC.h"
#import "HAMerchantAnalysisVC.h"
#import "HAPredationVC.h"
#import "HAMachineVC.h"
#import "HAInvitationVC.h"
#import "HAHoemAgentDetailsVC.h"
#import "HAServiceChargeVC.h"
#import "HATransactionLookVC.h"
#import "HARateRevisionVC.h"

@interface HAYJVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) HAYJMo *dataModel;


@end

@implementation HAYJVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    
    self.baseTLabel.text = @"业绩";
    self.baseNavView.hidden = YES;
    
    self.view.backgroundColor = [UIColor khaBGColor];
    
    [self loadData];
    [self initData];
    [self.tableView reloadData];
    
    // Do any additional setup after loading the view.
}


- (void)initData {
    [self.dataArray removeAllObjects];
    [self.dataArray addObject:@"mod1"];
    [self.tableView reloadData];
}

- (void)initUI{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-app_bottomH-49);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"mod1"]) {
        return 537+105+20;
    }
    return 0;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHYEJIID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHYEJIID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"mod1"]) {
        [self getMod1Cell:cell.contentView];
    }
    return cell;
}

-(void)loadData {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"v1/agent/index";
    if (!self.dataModel) {
        [MBProgressHUD showLoading:self.view];
    }
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.code == 1) {
                self.dataModel = [HAYJMo mj_objectWithKeyValues:dataModel.data];
                [self.tableView reloadData];
            }
        }
    }];
}

-(void)item2Click:(UITapGestureRecognizer *)tap {
    if (tap.view.tag == 0) {
        //代理管理
        HAHoemAgentVC *vc = [[HAHoemAgentVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag == 1) {
        //商户管理
        HAMyMerchantManageVC *vc = [[HAMyMerchantManageVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)item3Click:(UITapGestureRecognizer *)tap {
    if (tap.view.tag == 0) {
        //业绩统计 详情
        HAHoemAgentDetailsVC *vc = [[HAHoemAgentDetailsVC alloc]init];
        vc.agent_id = HAUserDefaults.agent_id;
        vc.name = HAUserDefaults.name;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag == 1) {
        //商户分析
        HAMerchantAnalysisVC *vc = [[HAMerchantAnalysisVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

//@[@"开通商户",@"邀请商户",@"邀请伙伴",@"新增伙伴",@"机具订货",@"机具管理",@"营销课堂",@"代理政策"]
//@[@"邀请伙伴",@"新增伙伴",@"机具订货",@"机具管理",@"营销课堂",@"",@"",@""]
-(void)item4Click:(UITapGestureRecognizer *)tap {
    NSInteger index = tap.view.tag;
    if (HAUserDefaults.isLock == 1) {
//        index = index+1;
        if (index == 0) {
            HAInvitationVC *vc = [[HAInvitationVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 1) {
            HAWebVC *vc = [[HAWebVC alloc]init];
            vc.tit = @"机具订货";
            vc.urlStr = [NSString stringWithFormat:@"%@index/item/index.html?token=%@",kHaBaseUrl,HAUserDefaults.token];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 2) {
        }else if (index == 3) {

        }else if (index == 4) {
        }
    }else {
        if (index == 0) {
            HAWebVC *vc = [[HAWebVC alloc]init];
            vc.tit = @"开通用户";
            vc.urlStr = [NSString stringWithFormat:@"%@index/merchant/canal.html?token=%@",kHaBaseUrl,HAUserDefaults.token];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 1) {
            HAWebVC *vc = [[HAWebVC alloc]init];
            vc.tit = @"邀请用户";
            vc.urlStr = [NSString stringWithFormat:@"%@index/merchant/invite.html?token=%@",kHaBaseUrl,HAUserDefaults.token];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 2) {
            HAWebVC *vc = [[HAWebVC alloc]init];
            vc.tit = @"邀请伙伴";
            vc.urlStr = [NSString stringWithFormat:@"%@index/invite/index.html?token=%@",kHaBaseUrl,HAUserDefaults.token];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 3) {
            HAInvitationVC *vc = [[HAInvitationVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 4) {
            HAWebVC *vc = [[HAWebVC alloc]init];
            vc.tit = @"机具订货";
            vc.urlStr = [NSString stringWithFormat:@"%@index/item/index.html?token=%@",kHaBaseUrl,HAUserDefaults.token];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 5) {
            // "机具管理"
            HAMachineVC *vc = [[HAMachineVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 6) {
            HAWebVC *vc = [[HAWebVC alloc]init];
            vc.tit = @"营销课堂";
            vc.urlStr = [NSString stringWithFormat:@"%@index/article/index.html?type=1&token=%@",kHaBaseUrl,HAUserDefaults.token];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 7) {
            HAWebVC *vc = [[HAWebVC alloc]init];
            vc.tit = @"代理政策";
            vc.urlStr = [NSString stringWithFormat:@"%@index/policy/index.html?token=%@",kHaBaseUrl,HAUserDefaults.token];
            [self.navigationController pushViewController:vc animated:YES];
        }

    }
}

-(void)jfImgView:(UITapGestureRecognizer*)tap {
    HAWebVC *vc = [[HAWebVC alloc]init];
    vc.tit = @"积分商城";
    vc.urlStr = [NSString stringWithFormat:@"%@index/store/index?token=%@",kHaBaseUrl,HAUserDefaults.token];
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)getMod1Cell:(UIView *)contentView {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    contentView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F5"];
    
    UIImageView *topImageView = [UIImageView ha_createImageView];
    topImageView.userInteractionEnabled = YES;
    topImageView.image = [UIImage imageNamed:@"yjbg"];
    [contentView addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_offset(360);
    }];
    
    CGFloat viewTop = app_topH+20;
    
    if (HAUserDefaults.isLock != 1) {
        UIImageView *LImgView = [self getM1ImgView:1];
        LImgView.tag = 0;
        [topImageView addSubview:LImgView];
        [LImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(viewTop);
            make.left.mas_equalTo(15);
            make.width.mas_equalTo((kScreenWidth-30-15)*0.5);
            make.height.mas_offset(114);
        }];

        UIImageView *RImgView = [self getM1ImgView:2];
        RImgView.tag = 1;
        [topImageView addSubview:RImgView];
        [RImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(viewTop);
            make.right.mas_equalTo(-15);
            make.width.mas_equalTo((kScreenWidth-30-15)*0.5);
            make.height.mas_offset(114);
        }];
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(item2Click:)];
        [LImgView addGestureRecognizer:tap1];

        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(item2Click:)];
        [RImgView addGestureRecognizer:tap2];

        viewTop += 114 + 20;
        viewTop += 20;
        
        UIView *icbg4View = [UIView ha_createView];
        [contentView addSubview:icbg4View];
        [icbg4View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(viewTop);
            make.centerX.mas_equalTo(0);
            make.width.mas_offset(134+134+36);
            make.height.mas_offset(44);
        }];
        
        [icbg4View removeAllSubviews];
        NSArray *title4List = @[@"业绩统计",@"商户分析"];
        NSMutableArray *view4List = [NSMutableArray array];
        for (int i = 0; i < title4List.count; i ++) {
            NSString *title = title4List[i];
            UIView *view = [UIView ha_createView];
            view.layer.cornerRadius = 20;
            view.layer.masksToBounds = YES;
            view.layer.borderColor = [UIColor whiteColor].CGColor;
            view.layer.borderWidth = 1;
            view.tag = i;
            
            UIImageView *icon = [UIImageView ha_createImageView];
            [view addSubview:icon];
            [icon mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_offset(13);
                make.height.mas_offset(13);
                make.centerY.mas_equalTo(0);
                make.left.mas_equalTo(24);
            }];
            
            if ([title isEqualToString:@"商户分析"]) {
                icon.image = [UIImage imageNamed:@"yjic_m3"];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(item3Click:)];
                [view addGestureRecognizer:tap];
            }else if ([title isEqualToString:@"业绩统计"]) {
                icon.image = [UIImage imageNamed:@"yjic_m4"];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(item3Click:)];
                [view addGestureRecognizer:tap];
            }
            
            UILabel *titlabel = [[UILabel alloc]init];
            titlabel.text = [NSString stringWithFormat:@"%@",title];
            titlabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
            titlabel.font = [UIFont systemFontOfSize:16];
            titlabel.textAlignment = 1;
            [view addSubview:titlabel];
            [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(0);
                make.left.equalTo(icon.mas_right).offset(6);
            }];
            [icbg4View addSubview:view];
            [view4List addObject:view];
        }
        
        [view4List mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:134 leadSpacing:0 tailSpacing:0];
        [view4List mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(44);
            make.top.mas_equalTo(0);
        }];

        viewTop += 44 + 25;
    }else {
        viewTop += 40;
    }
    
    
    UIView *d2View = [UIView ha_createView];
    [contentView addSubview:d2View];
    d2View.frame = CGRectMake(0, viewTop, kScreenWidth, 88*2+24);
    [d2View.superview bringSubviewToFront:d2View];
    [d2View removeAllSubviews];
    
    NSArray *titles = @[@"开通用户",@"邀请用户",@"邀请伙伴",@"新增伙伴",@"机具订货",@"机具管理",@"营销课堂",@"代理政策"];
    if (HAUserDefaults.isLock == 1) {
        titles = @[@"新增伙伴",@"机具订货",@"",@"",@"",@"",@"",@""];
    }
    NSMutableArray *views = [NSMutableArray array];
    
    for (int i = 0; i < titles.count; i ++) {
        UIView *view = [UIView ha_createView];
        view.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(item4Click:)];
        [view addGestureRecognizer:tap];
        
        UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"ha_%@",titles[i]]]];
        icon.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(5);
            make.width.mas_offset(42);
            make.height.mas_offset(42);
        }];
        
        UILabel *label = [UILabel ha_NLabelWithFont:16 textColor:@"#000000" text:titles[i] alignment:1];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(icon.mas_bottom).offset(10);
            make.right.left.equalTo(view);
        }];
        [d2View addSubview:view];
        [views addObject:view];
    }

    CGFloat spacing = 15;
    CGFloat tiemW = (kScreenWidth-spacing*5)/4;

    [[views subarrayWithRange:NSMakeRange(0, 4)] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:tiemW leadSpacing:spacing tailSpacing:spacing];
    [[views subarrayWithRange:NSMakeRange(0, 4)] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(88);
        make.top.mas_equalTo(0);
    }];
    
    [[views subarrayWithRange:NSMakeRange(4, 4)] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:tiemW leadSpacing:spacing tailSpacing:spacing];
    [[views subarrayWithRange:NSMakeRange(4, 4)] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(88);
        make.top.mas_equalTo(88+24);
    }];

    if (HAUserDefaults.isLock == 1) {
        viewTop += 88+24+35;
    }else {
        viewTop += 88*2+24+35;
    }

    UIImageView *jfImgView = [UIImageView ha_createImageView];
    jfImgView.layer.masksToBounds = YES;
    jfImgView.userInteractionEnabled = YES;
    jfImgView.image = [UIImage imageNamed:@"yjic_m2"];
    UITapGestureRecognizer *jftap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jfImgView:)];
    [jfImgView addGestureRecognizer:jftap];
    [contentView addSubview:jfImgView];
    [jfImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(viewTop);
        make.height.mas_offset(105);
    }];
    
    UILabel *jflabel = [[UILabel alloc]init];
    jflabel.textAlignment = 0;
    jflabel.textColor = [UIColor colorWithHexString:@"#222C35"];
    jflabel.font = [UIFont boldSystemFontOfSize:18];
    jflabel.text = @"积分兑换商城";
    [jfImgView addSubview:jflabel];
    [jflabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-5);
    }];
    
}

-(UIImageView *)getM1ImgView:(NSInteger)type {
    UIImageView *LImgView = [UIImageView ha_createImageView];
    LImgView.userInteractionEnabled = YES;
    LImgView.layer.cornerRadius = 12;
    LImgView.layer.masksToBounds = YES;
    LImgView.layer.borderColor = [UIColor whiteColor].CGColor;
    LImgView.layer.borderWidth = 1;

    
    UILabel *tlabel = [[UILabel alloc]init];
    tlabel.textAlignment = 1;
    tlabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    tlabel.font = [UIFont boldSystemFontOfSize:18];
    [LImgView addSubview:tlabel];
    [tlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(24);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-5);
    }];
    
    UILabel *clabel = [[UILabel alloc]init];
    clabel.textAlignment = 1;
    clabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    clabel.font = [UIFont boldSystemFontOfSize:22];
    [LImgView addSubview:clabel];
    [clabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tlabel.mas_bottom).offset(20);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-5);
    }];

    if (type == 1) {
//        LImgView.image = [UIImage imageNamed:@"bgs1"];
        tlabel.text = @"伙伴管理";
        if (![NSString isEmptyString:self.dataModel.agentCount]) {
            clabel.text = self.dataModel.agentCount;
        }
    }else if (type == 2) {
//        LImgView.image = [UIImage imageNamed:@"bgs2"];
        tlabel.text = @"用户管理";
        if (![NSString isEmptyString:self.dataModel.cusCount]) {
            clabel.text = self.dataModel.cusCount;
        }
    }
    return LImgView;
}





- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F5"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}


@end
